package com.hns.can.mainte.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.web.BaseController;
import com.hns.can.mainte.entity.AppCarMaintenInfoEntity;
import com.hns.can.mainte.service.ReportCarMainteInfoService;
import com.hns.can.mainte.vo.MaintenanceParamVo;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.pub.PubUtil;

/**
 * 车辆维保明细
 * @author:yecb
 * @email:lps00@163.com
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/reportCarMainteDetail")
public class ReportCarMainteInfoController extends BaseController  {
    
    @Autowired
    private ReportCarMainteInfoService reportCarMainteDetailService;

	/**
	 * 维保信息设置 - 新增维保信息
	 * @param entity 维保信息的实体
	 * @return
	 * @throws Exception
	 * @author:yecb
	 * @email:lps00@163.com
	 */
    @RequestMapping("/addMaintenanceInfo")
    @ResponseBody
    public Object addMaintenanceInfo(AppCarMaintenInfoEntity entity) throws Exception {
        reportCarMainteDetailService.addMaintenanceInfo(entity, getCurrentUser().getUser());
        return this.getReturnResult();
    }
    
    /**
     * 维保信息设置 - 修改维保信息
     * @param entity 维保信息的实体
     * @throws Exception
     * @author:yecb
     * @email:lps00@163.com
     */
    @RequestMapping("/uptMaintenanceInfo")
    @ResponseBody
    public Object uptMaintenanceInfo(AppCarMaintenInfoEntity entity) throws Exception {
        reportCarMainteDetailService.uptMaintenanceInfo(entity);
        return this.getReturnResult();
    }
    /**
     * 新增或者更新维保信息
     * @param entity
     * @return
     * @throws Exception
     * @author:yecb
     * @email:lps00@163.com
     */
    @RequestMapping("/saveOrUptMaintenanceInfo")
    @ResponseBody
    public Object saveOrUptMaintenanceInfo(AppCarMaintenInfoEntity entity) throws Exception {
        if(PubUtil.isEmpty(entity.getMaintenId())){
        	addMaintenanceInfo(entity);
        	return getReturnMsg("新增维保信息成功");
        }else{
        	uptMaintenanceInfo(entity);
        	return getReturnMsg("更新维保信息成功");
        }
    }
    
    /**
     *  删除单条维保信息
     * @param maintenId
     * @return
     * @throws Exception
     * @author:yecb
     * @email:lps00@163.com
     */
    @RequestMapping("/delMaintenanceInfo")
    @ResponseBody
    public Object delMaintenanceInfo(String maintenId) throws Exception {
        reportCarMainteDetailService.delMaintenanceInfo(maintenId);
        return getReturnMsg("删除维保信息成功");
    }
    
    /**
     * 维保信息维护 - 批量删除维保信息
     * @param maintenIds
     * @throws Exception
     * @author:yecb
     * @email:lps00@163.com
     */
    @RequestMapping("/delMaintenanceInfos")
    @ResponseBody
    public Object delMaintenanceInfos(@RequestParam("maintenIds") List<String> maintenIds) throws Exception {
        reportCarMainteDetailService.delMaintenanceInfos(maintenIds);
        return this.getReturnResult();
    }
    
    /**
     * 维保信息维护 - 查询维保信息
     * @param conditionVo
     * @param request
     * @param response
     * @throws Exception
     * @author:yecb
     * @email:lps00@163.com
     */
    @RequestMapping("/getMaintenancePage")
    @ResponseBody
    public Object getMaintenancePage(MaintenanceParamVo conditionVo) throws Exception {
        if( PubUtil.isEmpty(conditionVo.getCarIds()) || PubUtil.isEmpty(conditionVo.getMaintenCgy())){
        	throw new BusinessException(MsgType.WARN.getFlag(), "需要选择维保类别以及车辆!");
        }
    	SysUserEntity user = getCurrentUser().getUser();
        conditionVo.setCorpId(user.getCorpId());
        Pagination page = reportCarMainteDetailService.searchMaintenancePage(conditionVo);
        GridPage grid = new GridPage(page);
        return getReturnResult(grid);
    }
    /**
     * 根据主键查找维保记录
     * @param maintenId 主键
     * @return
     * @throws Exception
     * @author:yecb
     * @email:lps00@163.com
     */
    @RequestMapping("/getMaintenanceInfoById")
    @ResponseBody
    public Object getMaintenanceInfoById(String maintenId) throws Exception {
    	AppCarMaintenInfoEntity entity = reportCarMainteDetailService.getMaintenanceInfoByMaintenId(maintenId);
        return getReturnResult(entity);
    }
 
}
